package com.lgt_enterprise_android.view;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.lgt_enterprise_android.Constant;
import com.lgt_enterprise_android.R;
import com.lgt_enterprise_android.base.BaseActivity;
import com.lgt_enterprise_android.utils.CheckUpdate;
import com.lgt_enterprise_android.view.fragments.FirstFragment;
import com.lgt_enterprise_android.view.fragments.MeFragment;
import com.lgt_enterprise_android.view.fragments.StaffFragment;
import com.lgt_enterprise_android.view.pages.b.ArrangeJobActivity;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    private long mkeyTime;

    private Fragment mFirstFragment;
    private Fragment mSecondFragment;
    private Fragment mThirdFragment;
    private Fragment mMeFragment;

    private LinearLayout mTabFirst;
    private LinearLayout mTabSecond;
    private LinearLayout mTabThirid;
    private LinearLayout mTabForth;

    private ImageView mTabImg1;
    private ImageView mTabImg2;
    private ImageView mTabImg3;
    private ImageView mTabImg4;



    private TextView mTxtTab1;
    private TextView mTxtTab2;
    private TextView mTxtTab3;
    private TextView mTxtTab4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        log("token----"+Constant.token);
        isDarkfont = false;
        setContentView(R.layout.activity_main);
        super.onCreate(savedInstanceState);

        new MainActivityManager().bind(mContext);
        initView();
    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        int index=intent.getIntExtra("idx",-1);
        if(index>-1){
            setTabSelection(index);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        new CheckUpdate().checkUpgrade(this,false);

    }
    protected void initView() {

        mTabFirst = (LinearLayout) findViewById(R.id.linear_tab_1);
        mTabSecond = (LinearLayout) findViewById(R.id.linear_tab_2);
        mTabThirid = (LinearLayout) findViewById(R.id.linear_tab_3);
        mTabForth= (LinearLayout) findViewById(R.id.linear_tab_4);

        mTabImg1 = (ImageView) findViewById(R.id.tab_img_1);
        mTabImg2 = (ImageView) findViewById(R.id.tab_img_2);
        mTabImg3 = (ImageView) findViewById(R.id.tab_img_3);
        mTabImg4 = (ImageView) findViewById(R.id.tab_img_4);

        mTxtTab1 = (TextView) findViewById(R.id.tab_txt_1);
        mTxtTab2 = (TextView) findViewById(R.id.tab_txt_2);
        mTxtTab3 = (TextView) findViewById(R.id.tab_txt_3);
        mTxtTab4 = (TextView) findViewById(R.id.tab_txt_4);


        mTabFirst.setOnClickListener(this);
        mTabSecond.setOnClickListener(this);
        mTabThirid.setOnClickListener(this);
        mTabForth.setOnClickListener(this);

        setTabSelection(0);
    }

    private void setTabSelection(int index) {
        // 重置按钮
        resetBtn();
        // 开启一个Fragment事务
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        hideFragments(transaction);
        switch (index) {
            case 0:
                mTxtTab1.setTextColor(getResources().getColor(R.color.colorPrimary));
                mTabImg1.setImageDrawable(getResources().getDrawable(R.drawable.bottom_icon11));

                if (mFirstFragment == null) {
                    mFirstFragment = new FirstFragment();
                    transaction.add(R.id.fragment_content1, mFirstFragment);
                } else {
                    transaction.show(mFirstFragment);
                }

                break;
            case 1:
                mTxtTab2.setTextColor(getResources().getColor(R.color.colorPrimary));
                mTabImg2.setImageDrawable(getResources().getDrawable(R.drawable.bottom_icon22));
                if (mSecondFragment == null) {
                    mSecondFragment = new StaffFragment();
                    transaction.add(R.id.fragment_content1, mSecondFragment);
                } else {
                    transaction.show(mSecondFragment);
                }
                break;
            case 2:
//                Intent intent=new Intent(mContext, ArrangeJobActivity.class);
//                startActivity(intent);
//                mTxtTab3.setTextColor(getResources().getColor(R.color.colorPrimary));
//                mTabImg3.setImageDrawable(getResources().getDrawable(R.drawable.bottom_icon33));
//                if (mThirdFragment == null) {
//                    mThirdFragment = new ThirdFragment();
//                    transaction.add(R.id.fragment_content1, mThirdFragment);
//                } else {
//                    transaction.show(mThirdFragment);
//                }
                break;
            case 3:
                mTxtTab4.setTextColor(getResources().getColor(R.color.colorPrimary));
                mTabImg4.setImageDrawable(getResources().getDrawable(R.drawable.bottom_icon44));
                if (mMeFragment == null) {
                    mMeFragment = new MeFragment();
                    transaction.add(R.id.fragment_content1, mMeFragment);
                } else {
                    transaction.show(mMeFragment);
                }
                break;

        }
        transaction.commit();
    }

    private void hideFragments(FragmentTransaction transaction) {
        if (mFirstFragment != null) {
            transaction.hide(mFirstFragment);
        }
        if (mSecondFragment != null) {
            transaction.hide(mSecondFragment);
        }
        if (mThirdFragment != null) {
            transaction.hide(mThirdFragment);
        }

        if (mMeFragment != null) {
            transaction.hide(mMeFragment);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.linear_tab_1:
                setTabSelection(0);
                break;
            case R.id.linear_tab_2:
                setTabSelection(1);
                break;
            case R.id.linear_tab_3:
                Intent intent=new Intent(mContext, ArrangeJobActivity.class);
                intent.putExtra("type",1);
                startActivity(intent);
//                setTabSelection(2);
                break;
            case R.id.linear_tab_4:
                setTabSelection(3);
                break;
            default:
                break;
        }
    }

    private void resetBtn() {
        mTabImg1.setImageDrawable(getResources().getDrawable(R.drawable.bottom_icon1));
        mTabImg2.setImageDrawable(getResources().getDrawable(R.drawable.bottom_icon2));
        mTabImg3.setImageDrawable(getResources().getDrawable(R.drawable.bottom_icon3));
        mTabImg4.setImageDrawable(getResources().getDrawable(R.drawable.bottom_icon4));

        mTxtTab1.setTextColor(0xff666666);
        mTxtTab2.setTextColor(0xff666666);
        mTxtTab3.setTextColor(0xff666666);
        mTxtTab4.setTextColor(0xff666666);

    }

    @Override
    public void onBackPressed() {

        if(findViewById(R.id.frame_anim).getVisibility()==View.VISIBLE){
            findViewById(R.id.frame_anim).setVisibility(View.GONE);
            return;
        }

        // TODO Auto-generated method stub
        if ((System.currentTimeMillis() - mkeyTime) > 1500) {
            mkeyTime = System.currentTimeMillis();
            Toast.makeText(this, "再按一次退出应用", Toast.LENGTH_SHORT).show();
        } else {
            finish();
        }
    }


}
